package com.qian.apiserver.service.serviceImpl;

import com.qian.apiserver.dao.MatchRecordDao;
import com.qian.apiserver.entity.GmMatchRecord;
import com.qian.apiserver.model.*;
import com.qian.apiserver.service.ICountService;
import com.qian.apiserver.service.IUserService;
import com.qian.apiserver.service.MatchRecordServiceBase;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.qian.apiserver.service.IMatchRecordService;

import javax.persistence.criteria.*;
import java.math.BigDecimal;
import java.util.*;

@Service
public class MatchRecordServiceImpl extends MatchRecordServiceBase {
    @Autowired
    private MatchRecordDao dao;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICountService countService;
    public Map find(PageReq pageReq, final MatchRecordModel model) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "time"));
        Page<GmMatchRecord> page = dao.findAll(new Specification<GmMatchRecord>() {
            public Predicate toPredicate(Root<GmMatchRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Date> timePath = root.get("time");
                Path<BigDecimal> betting = root.get("betting");
                Path<BigDecimal> getChip = root.get("winChip");
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if(isNotEmpty(model.getUserId()))
                    condition = cb.and(condition,cb.equal(root.<Integer>get("userId"),model.getUserId()));
                if (isNotEmpty(model.getBeginDate())&& isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.between(timePath,model.getBeginDate(),model.getEndDate()));
                if(isNotEmpty(model.getUpBetting())&&isNotEmpty(model.getDownBetting()))
                    condition = cb.and(condition,cb.between(betting,model.getUpBetting(),model.getDownBetting()));
                if(isNotEmpty(model.getUpGetChip())&&isNotEmpty(model.getDownGetChip()))
                    condition = cb.and(condition,cb.between(getChip,model.getUpGetChip(),model.getDownGetChip()));
                else if(isNotEmpty(model.getUpGetChip()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(getChip,model.getUpGetChip()));
                else if(isNotEmpty(model.getDownGetChip()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(getChip,model.getDownGetChip()));
                else if(isNotEmpty(model.getUpBetting()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(betting,model.getUpBetting()));
                else if(isNotEmpty(model.getDownBetting()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(betting,model.getDownBetting()));
                else if (isNotEmpty(model.getBeginDate()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(timePath,model.getBeginDate()));
                else if (isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.lessThanOrEqualTo(timePath,model.getEndDate()));
                return condition;
            }
        },pageRequest);
        PageResp<MatchRecord> pageResp = page2pageResp(page);
        List<MatchRecord> list = new ArrayList<MatchRecord>();

        for(MatchRecord matchRecord:pageResp.getList()){
            MatchRecord model1 = new MatchRecord();
            User user  = userService.findById(matchRecord.getUserId());
            model1.setId(matchRecord.getId());
            model1.setUserId(matchRecord.getUserId());
            model1.setUserName(user.getUsername());
            model1.setUserCode(user.getUserCode());
            model1.setBetting(matchRecord.getBetting());
            model1.setWinChip(matchRecord.getWinChip());
            model1.setTime(matchRecord.getTime());
            list.add(model1);
        }
        Map map = new LinkedHashMap();
        map.put("totalPage",pageResp.getTotalPage());
        map.put("totalRow",pageResp.getTotalRow());
        map.put("pageSize",pageResp.getPageSize());
        map.put("pageNumber",pageResp.getPageNumber());
        map.put("list",list);
        return map;
    }

    //用户统计的定时任务
    public List<Count> findSechedule(PageReq pageReq,final MatchRecordModel model) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "time"));
        Page<GmMatchRecord> page = dao.findAll(new Specification<GmMatchRecord>() {
            public Predicate toPredicate(Root<GmMatchRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Date> timePath = root.get("time");
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if (isNotEmpty(model.getBeginDate()) && isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition, cb.between(timePath, model.getBeginDate(), model.getEndDate()));
                return condition;
            }
        }, pageRequest);
        PageResp<MatchRecord> pageResp = page2pageResp(page);
        List<Count> list = new ArrayList<Count>();
        Double bettingAmount = 0.00;
        Double winChipAmount = 0.00;
        for(MatchRecord matchRecord:pageResp.getList()) {
           bettingAmount += matchRecord.getBetting().doubleValue();
           winChipAmount += matchRecord.getWinChip().doubleValue();
        }
        Count count = new Count();
        count.setBetting(new BigDecimal(bettingAmount));
        count.setWinChip(new BigDecimal(winChipAmount));
        count.setTime(new Date());
        count.setCode(model.getCode());
        countService.save(count);
        list.add(count);
        return list;
    }

    public Count allMinute(PageReq pageReq,final MatchRecordModel model) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "time"));
        Page<GmMatchRecord> page = dao.findAll(new Specification<GmMatchRecord>() {
            public Predicate toPredicate(Root<GmMatchRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Date> timePath = root.get("time");
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if (isNotEmpty(model.getBeginDate()) && isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition, cb.between(timePath, model.getBeginDate(), model.getEndDate()));
                return condition;
            }
        }, pageRequest);
        PageResp<MatchRecord> pageResp = page2pageResp(page);
        Double bettingAmount = 0.00;
        Double winChipAmount = 0.00;
            for (MatchRecord matchRecord : pageResp.getList()) {
                bettingAmount += matchRecord.getBetting().doubleValue();
                winChipAmount += matchRecord.getWinChip().doubleValue();
            }
            Count count = new Count();
            count.setBetting(new BigDecimal(bettingAmount));
            count.setWinChip(new BigDecimal(winChipAmount));
            count.setTime(model.getEndDate());
//            list.add(count);
        return count;
    }

    public List findName(PageReq pageReq,final MatchRecordModel model) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "time"));
        Page<GmMatchRecord> page = dao.findAll(new Specification<GmMatchRecord>() {
            public Predicate toPredicate(Root<GmMatchRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Date> timePath = root.get("time");
                Path<BigDecimal> betting = root.get("betting");
                Path<BigDecimal> getChip = root.get("winChip");
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if(isNotEmpty(model.getUserId()))
                    condition = cb.and(condition,cb.equal(root.<Integer>get("userId"),model.getUserId()));
                if (isNotEmpty(model.getBeginDate())&& isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.between(timePath,model.getBeginDate(),model.getEndDate()));
                if(isNotEmpty(model.getUpBetting())&&isNotEmpty(model.getDownBetting()))
                    condition = cb.and(condition,cb.between(betting,model.getUpBetting(),model.getDownBetting()));
                if(isNotEmpty(model.getUpGetChip())&&isNotEmpty(model.getDownGetChip()))
                    condition = cb.and(condition,cb.between(getChip,model.getUpGetChip(),model.getDownGetChip()));
                else if(isNotEmpty(model.getUpGetChip()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(getChip,model.getUpGetChip()));
                else if(isNotEmpty(model.getDownGetChip()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(getChip,model.getDownGetChip()));
                else if(isNotEmpty(model.getUpBetting()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(betting,model.getUpBetting()));
                else if(isNotEmpty(model.getDownBetting()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(betting,model.getDownBetting()));
                else if (isNotEmpty(model.getBeginDate()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(timePath,model.getBeginDate()));
                else if (isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.lessThanOrEqualTo(timePath,model.getEndDate()));
                return condition;
            }
        },pageRequest);
        PageResp<MatchRecord> pageResp = page2pageResp(page);
        List<MatchRecord> list = new ArrayList<MatchRecord>();

        for(MatchRecord matchRecord:pageResp.getList()){
            MatchRecord model1 = new MatchRecord();
            User user  = userService.findById(matchRecord.getUserId());
            model1.setId(matchRecord.getId());
            model1.setUserId(matchRecord.getUserId());
            model1.setUserName(user.getUsername());
            model1.setUserCode(user.getUserCode());
            model1.setBetting(matchRecord.getBetting());
            model1.setWinChip(matchRecord.getWinChip());
            model1.setTime(matchRecord.getTime());
            list.add(model1);
        }
        return list;
    }
}